var data = {};
function bindSaveBtnClick() {
	// 保存地址的调用方法，先偷懒没有独立文件
	$("#saveAddressBtn").click(function() {
		data.id = $("input[name='id']").val();
		data.contactor = $("input[name='contactor']").val();
		data.phone = $("input[name='phone']").val();
		data.address = $("input[name='address']").val();
		data.contactor = $("input[name='contactor']").val();
    	data.defaultAddr = $("input[name='defaultAddr']").prop("checked") ? 1 : 2;
		$.ajax({type:"POST", url: "/address/save.html",
			data: data,
			dataType: "json",
			success: function(d) {
				if(d && d.code == 1) {
					gshopLayer.success();
					// 如果没有定义回调处理，则跳转到列表页
					"undefined" == typeof addAddrCallback ? window.location.href = "/address/list.html" : addAddrCallback(data);
				} else {
					gshopLayer.fail(d.msg);
				}
			}, beforeSend : function() {
				layerLoading = gshopLayer.loading("操作中，请稍后…");
			}, error : function() {
				gshopLayer.fail();
			}, complete : function() {
				// 关闭加载层
				layer.close(layerLoading);
			}
		});
	});
};
// 绑定在个人中心跳转到列表点击新增地址按钮时的事件处理
$("#addressAddBtn").click(function(){
	window.location.href= "/address/add.html";
});
/**
 * 列表UL的点击事件，针对来源不同页面做不同的处理
 */
function bindAddressUlClick() {
	$(".address-wapper ul[name='addrUl']").click(function() {
		var page = $("#page").val();
		switch (page) {
		case "sett":
			var addrs = [];
//			$(this).find(".address-detail-left p").each(function(){
//				addrs.push($(this).html());
//			});
//			showAddress($(this).find(".address-name span").html(), addrs.join(""));
			showAddress($(this).find(".address-name span").html(), $(this).find(".address-detail-left p").first().html(), $(this).find(".address-detail-left p").last().html());
			break;
		default:
			window.location.href= "/address/edit/"+$(this).attr("addrId")+".html";
			break;
		}
	});
}

//记录选择的省市区县，便于回显
var chooseProvValue = "";
var layerArea;
var layerLoading;
/**
 * 选择省市区县事件
 * @param id		id
 * @param level	所属层级
 */
function chooseProv(id, level) {
	$.ajax({type:"POST", url: "/area/"+id+".html",
		dataType: "json",
		success: function(data) {
			if(data) {
				// 组装html
				var html = [];
				html.push('<div class="new-header"><a href="javascript:closeAreaLayer()" class="new-a-back"><span>关闭</span></a>');
				html.push('<h2>选择'+level.name+'</h2></div>');
				html.push('<div class="address-wapper"><ul>');
				for(var i in data) {
					html.push('<li class="address-name address-provs" areaId="'+data[i].id+'">'+data[i].name+'</li>');
				}
				html.push('</ul></div>');
				
				// 区县则关闭弹窗
				layer.close(layerArea);
				layerArea = gshopLayer.fullScreen({html: html.join("")});
				
				// 设置本级点击事件，因为实际为ajax获取，因此不会出现多次绑定事件的问题。
				$(".address-provs").click(function(){
					var areaId = $(this).attr("areaId");
					var areaName = $(this).text();
					chooseProvValue += areaName +" ";
					var nextLevel = getNextLevel(level, areaId, areaName);
					if(nextLevel) {
						chooseProv(areaId, nextLevel);
					}
				});
			} else {
				gshopLayer.fail(data.msg);
			}
		}, beforeSend : function() {
			layerLoading = gshopLayer.loading("操作中，请稍后…");
		}, error : function() {
			gshopLayer.fail();
		}, complete : function() {
			// 关闭加载层
			layer.close(layerLoading);
		}
	});
	
}
/**
 * 获取下一级编码，以及回显对应的值
 * @param level	当前级别
 * @param id		选择的省市区id
 * @param name	选择的省市区名称
 * @returns {String}
 */
function getNextLevel(level, id, name) {
	if(!level || !level.code) level = {code:""};
	switch (level.code) {
		case "prov":
			data.provinceId = id;
			data.provinceName = name;
			return {code:"city", name:"城市"};
		case "city":
			data.cityId = id;
			data.cityName = name;
			return {code:"county", name:"区县"};
		case "county":
			data.countryId = id;
			data.countryName = name;
			$("#chooseProv input").val(chooseProvValue);
			chooseProvValue = "";
			// 区县则关闭弹窗
			layerArea && layer.close(layerArea);
			return;
		default:
			return {code:"prov", name:"省份"};
	}
}
/**
 * 点击关闭按钮之后的触发事件
 */
function closeAreaLayer () {
	// 关闭弹出层
	layer.close(layerArea);
	// 清空已选择的值
	chooseProvValue = "";
	data = {};
}

$("#chooseProv").click(function(){
	chooseProv(0, getNextLevel());
});
bindSaveBtnClick();
bindAddressUlClick();